part of 'component.dart';

class RadioItem extends StatefulWidget {
  const RadioItem({
    Key? key,
    required this.text,
    required this.value,
    this.groupValue,
    this.onTap,
    this.iconSize,
    this.fontSize,
    this.selectedColor,
    this.isCircle = true,
  }) : super(key: key);
  final int value;
  final int? groupValue;
  final String text;
  final Function(bool)? onTap;
  final double? iconSize;
  final double? fontSize;
  final Color? selectedColor;
  final bool isCircle;
  @override
  State<RadioItem> createState() => _RadioItemState();
}

class _RadioItemState extends State<RadioItem>
    with AutomaticKeepAliveClientMixin {
  bool isSelected = false;

  @override
  void initState() {
    super.initState();
    isSelected = widget.value == widget.groupValue;
  }

  @override
  void didUpdateWidget(RadioItem oldWidget) {
    super.didUpdateWidget(oldWidget);
    isSelected = widget.value == widget.groupValue;
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);

    var selectedColor = widget.selectedColor ?? Colors.blue;
    return InkWell(
      onTap: () {
        setState(() {
          if (widget.onTap != null) {
            isSelected = !isSelected;
            widget.onTap!(isSelected);
          }
        });
      },
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Icon(
            isSelected
                ? (widget.isCircle ? Icons.check_circle : Icons.check_box)
                : (widget.isCircle
                    ? Icons.circle_outlined
                    : Icons.check_box_outline_blank),
            size: widget.iconSize ?? 22.r,
            color: isSelected ? selectedColor : const Color(0xff999999),
          ),
          SizedBox(
            width: 3.r,
          ),
          Flexible(
            child: Text(
              widget.text,
              style: TextStyle(
                color: isSelected ? selectedColor : const Color(0xff333333),
                fontSize: widget.fontSize ?? 15.sp,
              ),
            ),
          )
        ],
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
